unit devrun;

interface

uses
  Classes, Windows, Dialogs, utils;

type
  TLineOutputEvent = procedure(Sender: TObject; const Line: String) of Object;

  TDevRun = class(TThread)
  private
    TheMsg     : String;
    CurrentLine: String;
    FLineOutput: TLineOutputEvent;
    fCheckAbort: TCheckAbortFunc;
  protected
    procedure CallLineOutputEvent;
    procedure Execute; override;
    procedure LineOutput(Line: String);
    procedure ShowError(Msg: String);
    procedure ShowMsg;
  public
    Command   : string;
    Directory : string;
    Output    : string;
    property OnLineOutput: TLineOutputEvent read FLineOutput write FLineOutput;
    property OnCheckAbort: TCheckAbortFunc read FCheckAbort write FCheckAbort;
  end;

implementation

procedure TDevRun.ShowMsg;
begin
  utils.ShowError(TheMsg);
end;

procedure TDevRun.ShowError(Msg: String);
begin
  TheMsg := Msg;
  Synchronize(ShowMsg);
end;

procedure TDevRun.CallLineOutputEvent;
begin
    FLineOutput(Self, CurrentLine);
end;

procedure TDevRun.LineOutput(Line: String);
begin
  CurrentLine := Line;
  if Assigned(FLineOutput) then
      Synchronize(CallLineOutputEvent);
end;

procedure TDevRun.Execute;
begin
  Output := RunAndGetOutput(Command, Directory, Self.ShowError,
    LineOutput, FCheckAbort);
end;

end.
